/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared.sources;

import avl.sv.shared.XML.SolutionXML_Parser;
import avl.sv.shared.fileFilters.SolutionFilter;
import avl.sv.shared.Solution;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 *
 * @author benbryan
 */
public class SolutionSourceFile extends SolutionSource {

    private final File file;

    public SolutionSourceFile(File solutionFile){
        super(solutionFile.getName(), 0);
        this.file = solutionFile;
    }

    private String readFile(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");

        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }

        return stringBuilder.toString();
    }

    @Override
    public String setSolution(String annotations) {
        String annotationsName = getName().substring(0, getName().lastIndexOf(".")) + SolutionFilter.getDefaultExention();
        File f = new File(file.getParent() + File.separator + annotationsName);
        if (f.canWrite()) {
            try {
                try (FileWriter fw = new FileWriter(f)) {
                    fw.write(annotations);
                }
            } catch (IOException ex) {
                Logger.getLogger(SolutionSourceFile.class.getName()).log(Level.SEVERE, null, ex);
                return "error: cannot write file";
            }
        } else {
            System.out.println("Cannot write to annotation file: " + f.toString());
            return "error: cannot write to file";
        }
        return "success";
    }
    
    @Override
    public Solution getSolution() {
        try {
            String xml;
            if (file.canRead()) {
                xml = readFile(file);
            } else {
                return null;
            }
            if (xml.equals("")) {
                return null;
            }
            SolutionXML_Parser parser = new SolutionXML_Parser();
            Solution solution = parser.parse(xml);
            return solution;
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(SolutionSource.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static boolean exists(File file) {
        return file.exists();
    }

    @Override
    public String delete() {
        file.delete();
        return "Success";
    }
}
